class EventosController < ApplicationController
  # GET /eventos
  # GET /eventos.xml
  before_filter :administrador_eventos_logeado,:only => [:index]
  before_filter :administrador_eventos_deslogeado,:only => [:index_logeado]
  layout 'plantillaS'
  #require 'xmlsimple'


  def index
    #@eventos = Evento.find_by_sql("SELECT e.id as id, e.nombre as nombre, e.fecha_inicio as fecha_inicio, e.hora_inicio as hora_inicio, c.nombre as nombre_categoria FROM eventos e, categorias c WHERE e.categoria_id = c.id LIMIT 10")
    #@categorias = Categoria.all
    #@participante = Participante.all
    @eventos = Evento.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @eventos }
    end
  end

  def eventos_logeado
    @eventos = Evento.find_by_sql("SELECT e.id as id, e.nombre as nombre, e.fecha_inicio as fecha_inicio, e.hora_inicio as hora_inicio, c.nombre as nombre_categoria FROM eventos e, categorias c WHERE e.categoria_id = c.id LIMIT 10")
    @categorias = Categoria.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @eventos }
    end
  end

  # GET /eventos/1
  # GET /eventos/1.xml
  def show
    @evento = Evento.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @evento }
    end
  end

  def show_filter
    @idCategoria = params[:id]
    @categoria = Categoria.find(:first, :conditions => ["id = ?", @idCategoria])
    @eventos = Evento.find(:all, :conditions => ["categoria_id = ?", @idCategoria], :order => 'fecha_inicio', :limit => 10)
    @categorias = Categoria.all
    respond_to do |format|
      logger.info 'Todos los querys se ejecutaron correptamente'
      format.html # show.html.erb
      format.xml  { render :xml => @eventos }
    end
  end

  # GET /eventos/new
  # GET /eventos/new.xml
  def new
    @evento = Evento.new
    @categorias = Categoria.all
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @evento }
    end
  end

  # GET /eventos/1/edit
  def edit
    @evento = Evento.find(params[:id])
    @categorias = Categoria.all
  end

  # POST /eventos
  # POST /eventos.xml
  def create
    @evento = Evento.new(params[:evento])

    respond_to do |format|
      if @evento.save
        logger.info 'El evento se creo correptamente'
        flash[:notice] = 'Evento was successfully created.'
        format.html { redirect_to(@evento) }
        format.xml  { render :xml => @evento, :status => :created, :location => @evento }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @evento.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /eventos/1
  # PUT /eventos/1.xml
  def update
    @evento = Evento.find(params[:id])

    respond_to do |format|
      if @evento.update_attributes(params[:evento])
        logger.info 'El evento se elimino correptamente'
        flash[:notice] = 'Evento was successfully updated.'
        format.html { redirect_to(@evento) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @evento.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /eventos/1
  # DELETE /eventos/1.xml
  def destroy
    @evento = Evento.find(params[:id])
    @evento.destroy

    respond_to do |format|
      format.html { redirect_to(eventos_url) }
      format.xml  { head :ok }
    end
  end


  
end
